package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.coolsk.dao.ScenicDao;
import cn.sumpu.app.coolsk.domain.Scenic;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class ScenicDaoImpl extends SqlSessionDaoSupport implements ScenicDao {
	
	private final static Logger logger = LoggerFactory.getLogger(ScenicDaoImpl.class);
	
	@Override
	public void insertScenic(Scenic scenic) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.ScenicMapper.insertScenic",scenic);
		} catch (Exception e) {
			
			if (logger.isErrorEnabled()) {
				logger.error("insertScenic failure.", e);
			}
			throw new AppBusinessException("insertScenic failure.", e);
		}
	}

	@Override
	public void updateScenic(Scenic scenic) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.ScenicMapper.updateScenic",scenic);
		} catch (Exception e) {
			
			if (logger.isErrorEnabled()) {
				logger.error("updateScenic failure.", e);
			}
			throw new AppBusinessException("updateScenic failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Scenic> getScenic(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.ScenicMapper.getScenic",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getScenic failure.", e);
			}
			throw new AppBusinessException("getScenic failure.", e);
		}
	}

	@Override
	public int getScenicCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.ScenicMapper.getScenicCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getScenicCount failure.", e);
			}
			throw new AppBusinessException("getScenicCount failure.", e);
		}
	}

	@Override
	public void delScenic(Map<String, Object> map) throws AppBusinessException {
		try {
			 getSqlSession().delete("cn.sumpu.app.coolsk.persistence.ScenicMapper.delScenic",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delScenic failure.", e);
			}
			throw new AppBusinessException("delScenic failure.", e);
		}

	}

	@Override
	public double getScenicPriceMin(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.ScenicMapper.getScenicPriceMin",map);
			return obj==null?0:(Double)obj;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getScenicPriceMin failure.", e);
			}
			throw new AppBusinessException("getScenicPriceMin failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Scenic> searchScenic(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.ScenicMapper.searchScenic",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getScenic failure.", e);
			}
			throw new AppBusinessException("getScenic failure.", e);
		}
	}

	@Override
	public int searchScenicCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.ScenicMapper.searchScenicCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getScenicCount failure.", e);
			}
			throw new AppBusinessException("getScenicCount failure.", e);
		}
	}
	
	

}
